package api

import (
	"encoding/json"
	"fmt"
	"time"
)

var (
	datetime string
)

func init() {
	now := time.Now()
	year := now.Year()
	month := int(now.Month())
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()

	datetime = fmt.Sprintf("%d-%d-%d %d:%d:%d", year, month, day, hour, minute, second)
	fmt.Printf("现在的时间是：%v\n", datetime)
	time.Sleep(time.Millisecond)
	now = time.Now()
	datetime = now.Format("2006-01-02 15:04:05")
	fmt.Printf("现在的时间是：%v\n", datetime)
}

func Welcome() {
	fmt.Printf("欢迎陈文小超来到%v的golang语言学习……\n", datetime)
}

// Access 程序练习入口
func Access() {
	structStudy()
}

type Person struct {
	Name   string
	Age    int
	Scores [5]float64
	ptr    *int
	slice  []int
	map1   map[string]string
}

type Monster struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	Skill string `json:"skill"`
}

// 结构体练习
func structStudy() {
	/*var p1 Person
	p1.slice = make([]int, 10)
	p1.slice[0] = 100
	p1.map1 = make(map[string]string)
	p1.map1["key1"] = "value1"
	fmt.Println(p1)*/

	monster := Monster{"牛魔王", 55, "芭蕉扇"}
	jsonStr, err := json.Marshal(monster) // 返回的是整型切片[]uint8
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%T\n", jsonStr)
		fmt.Println(string(jsonStr))
	}

	fmt.Println("--------------------")

	// 方法
	var function Function
	function.Name = "Function"
	function.test()
	fmt.Println("--------------------")
}

type Function struct {
	Name string
}

func (function Function) test() {
	fmt.Println("test()Name = ", function.Name)
}
